/*
 *    Alliciga's Process Partner Project
 * 
 *  Copyright (C) 2006-2011 Alliciga <alliciga@users.sourceforge.net>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 */

#include "../include/main.h"
#include "../include/MonitorThread.h"
#include "../include/UserInterfaceThread.h"
#include "../include/version.h"

int main(int argc, char** argv)
{
 	ppp oppp;
 	if(!oppp.HandleOptions(argc,argv))
		return 0;
 	if(!oppp.run())
		return 0;
	return 0;
}

ppp::ppp()
{
	level = 30;
}

ppp::~ppp()
{
	monitorThreadList.clear();
}

bool ppp::run()
{

	UserInterfaceThread *pUIThread = new UserInterfaceThread(*this);
	pUIThread->run();
	pUIThread->join();
	if(pUIThread)
		delete pUIThread;

	return true;
}

bool ppp::HandleOptions(int ac, char** av)
{
	boost::program_options::options_description desc("Allowed options");
	desc.add_options()
		("help", "show help message")
		("version", "show version info")
		("level", boost::program_options::value<int>(), "set monitor level (1..86400) default is 30")
	;
	
	boost::program_options::variables_map vm;
	boost::program_options::store(boost::program_options::parse_command_line(ac,av,desc),vm);
	boost::program_options::notify(vm);
	
	if(vm.count("help"))
	{
		cout << desc << endl;
		return false;
	}
	if(vm.count("version"))
	{
		cout << PRODUCT_NAME << " "
			<< MAJOR_VERSION << "." << MINOR_VERSION
			<< endl << flush;
		return false;
	}
	if(vm.count("level"))
	{
		int lv = vm["level"].as<int>();
		if(lv<1||lv>86400)
		{
			cout << "level should between 1 and 86400" << endl;
			return false;
		}
		cout << "Monitor level was set to "
			<< lv << endl;
		level = lv;
	}
	else
	{
		cout << "Monitor level not set, using default level = 30" << endl;
	}
	return true;
}

bool ppp::AddMonitorProcess(int pid)
{
	MonitorThreadList::iterator r = monitorThreadList.begin();

	while(monitorThreadList.end() != r)
	{
		if(r->GetProcessID()==pid)
		{
			return false;
		}
		r++;
	}
	MonitorThread * pMT = new MonitorThread(pid,level);
	if(pMT->GetProcessName()=="unknown")
	{
		cout << "NonExisted Process" << endl;
		delete pMT;
		return false;
	}
	monitorThreadList.push_front(*pMT);
	delete pMT;

	MonitorThreadList::iterator r0 = monitorThreadList.begin();

	while(monitorThreadList.end() != r0)
	{
		if(r0->GetProcessID()==pid)
		{
			r0->run();
		}
		r0++;
	}
	return true;
}

bool ppp::DeleteMonitorProcess(int pid)
{
	MonitorThreadList::iterator r = monitorThreadList.begin();
	while(monitorThreadList.end() != r)
	{
		if(r->GetProcessID()==pid)
		{
			monitorThreadList.erase(r);
		}
		r++;
	}
	return true;
}

bool ppp::AddMonitorPolicy(int pid, string mpName)
{
	MonitorThreadList::iterator r = monitorThreadList.begin();
	while(monitorThreadList.end() != r)
	{
		if(r->GetProcessID()==pid)
		{
			return r->AddMonitorPolicy(mpName);
		}
		r++;
	}
	return false;
}

bool ppp::DeleteMonitorPolicy(int pid, string mpName)
{
	MonitorThreadList::iterator r = monitorThreadList.begin();
	while(monitorThreadList.end() != r)
	{
		if(r->GetProcessID()==pid)
		{
			return r->DeleteMonitorPolicy(mpName);
		}
		r++;
	}
	return false;
}

bool ppp::DeleteNotificationPolicy(int pid, string mpName, string npName)
{
	MonitorThreadList::iterator r = monitorThreadList.begin();
	while(monitorThreadList.end() != r)
	{
		if(r->GetProcessID()==pid)
		{
			return r->DeleteNotificationPolicy(mpName, npName);
		}
		r++;
	}
	return false;
}

list<string> ppp::GetMonitorPolicyList(int pid)
{
	list<string> tmp;
	MonitorThreadList::iterator r = monitorThreadList.begin();
	while(monitorThreadList.end() != r)
	{
		if(r->GetProcessID()==pid)
		{
			return r->GetEnabledMonitorPolicy();
		}
		r++;
	}
	return tmp;
}

bool ppp::AddNotificationPolicy(int pid, string mpName, string npName)
{
	MonitorThreadList::iterator r = monitorThreadList.begin();
	while(monitorThreadList.end() != r)
	{
		if(r->GetProcessID()==pid)
		{
			return r->AddNotificationPolicy(mpName,npName);
		}
		r++;
	}
	return false;
}

list<string> ppp::GetNotificationPolicyList(int pid, string mpName)
{
	list<string> tmp;
	MonitorThreadList::iterator r = monitorThreadList.begin();
	while(monitorThreadList.end() != r)
	{
		if(r->GetProcessID()==pid)
		{
			return r->GetEnabledNotificationPolicy(mpName);
		}
		r++;
	}
	return tmp;
}

list<string> ppp::GetMonitorProcessList()
{
	list<string> ret;
	MonitorThreadList::iterator r = monitorThreadList.begin();
	while(monitorThreadList.end() != r)
	{
		ret.push_front(r->GetProcessName());
		r++;
	}
	return ret;
}

list<string> ppp::GetAvailableMonitorPolicy()
{
	list<string> tmp;
	tmp.push_front("BaseMonitorPolicy");
	tmp.push_front("MemoryMonitorPolicy");
	return tmp;
}

list<string> ppp::GetAvailableNotificationPolicy()
{
	list<string> tmp;
	tmp.push_front("Console");
	return tmp;
}
 
//---------- End of file ---------
